package cn.alibab.cangku.service.impl;


import cn.alibab.cangku.dao.CreateId;
import cn.alibab.cangku.dao.ProductDao;
import cn.alibab.cangku.mapper.BizConsumerMapper;
import cn.alibab.cangku.mapper.BizOutStockMapper;
import cn.alibab.cangku.mapper.BizProductStockMapper;
import cn.alibab.cangku.model.BizConsumer;
import cn.alibab.cangku.model.BizOutStock;
import cn.alibab.cangku.model.BizProductStock;
import cn.alibab.cangku.service.ProductOutputService;
import cn.alibab.cangku.vo.ProductOutputVo;
import cn.alibab.common.model.R;
import cn.alibab.common.model.RCode;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductOutputServiceImpl implements ProductOutputService {
    private final ProductDao productDao;
    private final BizConsumerMapper bizConsumerMapper;
    private final BizOutStockMapper bizOutStockMapper;
    private final BizProductStockMapper bizProductStockMapper;
    private final CreateId createId;
    public ProductOutputServiceImpl(ProductDao productDao, BizConsumerMapper bizConsumerMapper, BizOutStockMapper bizOutStockMapper, BizProductStockMapper bizProductStockMapper, CreateId createId) {
        this.productDao = productDao;
        this.bizConsumerMapper = bizConsumerMapper;
        this.bizOutStockMapper = bizOutStockMapper;
        this.bizProductStockMapper = bizProductStockMapper;
        this.createId = createId;
    }
    //查询所有出库产品
    @Override
    public R getAllOutProduct() {
        List<ProductOutputVo> allOutProduct = productDao.getAllOutProduct();
        for(int i = 0;i<allOutProduct.size();i++){
            System.out.println(allOutProduct.get(i).getOutNum());
            System.out.println(allOutProduct.get(i).getProductNumber());
            System.out.println(allOutProduct.get(i).getpName());
        }
        return R.ok(allOutProduct);
    }
    //物资出库
    @Override
    public R delProductStock(ProductOutputVo productOutputVo) {
//        System.out.println(productOutputVo.getpName());
        BizOutStock bizOutStock = new BizOutStock();
        BizConsumer bizConsumer = new BizConsumer();
        BeanUtils.copyProperties(productOutputVo,bizConsumer);
        BeanUtils.copyProperties(productOutputVo,bizOutStock);
        bizConsumer.setCreateTime(new Date());
        bizConsumer.setModifiedTime(new Date());
        bizConsumer.setSort(1);
        bizConsumerMapper.insertSelective(bizConsumer);
        Long id= bizConsumer.getId();
        bizOutStock.setConsumerId(id);
        bizOutStock.setCreateTime(new Date());
        bizOutStock.setOperator("123");
        bizOutStock.setOutNum(createId.getId(1));
        bizOutStock.setStatus(0);
        bizOutStock.setType(1);
        bizOutStock.setPriority(1);
        bizOutStockMapper.insertSelective(bizOutStock);
//        System.out.println(productNumber);
        //获取出库的物资名称
        String pName = productOutputVo.getpName();
//        System.out.println(pName+"=====333333");
        BizProductStock productStockByPNume = (BizProductStock)findProductStockByPName(pName).getData();
//        System.out.println(productStockByPNume.getStock()+"=====11111111111");
        // 获取出库的数量
        Integer productNumber = productOutputVo.getProductNumber();
        Long stock = productStockByPNume.getStock();
        System.out.println(stock);
        if (productNumber > stock ){
            return R.error(RCode.STOCK_ERROR);
        }else if (stock == 0){
            return R.error(RCode.STOCK_ERROR);
        }else{
            productStockByPNume.setStock(stock-productNumber);
            bizProductStockMapper.updateByPrimaryKeySelective(productStockByPNume);
            return R.ok();
        }
    }
    //通过pName获取对应的物资库存
    public R findProductStockByPName(String pName ){
//        System.out.println("11111=====2222222222");
//        System.out.println(pName+"11111=====2222222222");
        BizProductStock productStockByPName = productDao.findProductStockByPName(pName);
//        System.out.println(productStockByPName.getStock()+"=====2222222222");
        return R.ok(productStockByPName);
    }
}
